package tech.aistar.day09.homework;

import java.util.*;
import java.util.Map.Entry;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2022/7/29 8:35 上午
 */
public class PurcaseTest {
    public static void main(String[] args) {
        List<Purcase> list = new ArrayList<>();
        Purcase p1 = new Purcase("宝洁","洗手粉",18.5);
        Purcase p2 = new Purcase("联合利华","肥皂",4.5);
        Purcase p3 = new Purcase("宝洁","牙膏",32.5);
        Purcase p4 = new Purcase("宝洁","毛巾",14.5);
        Purcase p5 = new Purcase("洁利","洗面奶",26.0);
        Purcase p6 = new Purcase("好迪","洗发水",27.5);
        Purcase p7 = new Purcase("多芬","沐浴露",38.5);
        Purcase p8 = new Purcase("宝洁","洗洁精",3.4);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);

        countPrice2(list);
    }

    //自定义对象
    private static void countPrice2(List<Purcase> list) {
        //1. 将同一品牌的purcase进行归类存储
        //key - 品牌
        //value - 该品牌对应的集合对象 - n个p
        Map<String,List<Purcase>> maps = new HashMap<>();

        //遍历list
        for (Purcase p : list) {
            String brand = p.getBrand();//品牌

            //判断该集合中是否已经存在该品牌
            if(maps.containsKey(brand)){
                //根据key来获取value
                List<Purcase> purcases = maps.get(brand); // Integer count = maps.get(随机数);
                purcases.add(p);//         count++     maps.put(随机数4,count);

                //maps.put(brand,pList);
            }else{

                //maps.put(随机数,1);

                List<Purcase> pList = new ArrayList<>();
                pList.add(p);
                maps.put(brand,pList);
            }
        }

        List<PurcaseVo> voList = new ArrayList<>();

        //统计
        Set<String> set = maps.keySet();
        for (String b : set) {
            double total = 0.0d;
            List<Purcase> lists = maps.get(b);
            for (Purcase purcase : lists) {
                total+=purcase.getCost();
            }

            PurcaseVo vo = new PurcaseVo(b,total);
            voList.add(vo);
        }

       voList.sort((e1,e2)-> (int) (e2.getTotal()-e1.getTotal()));

        voList.forEach(e-> System.out.println(e.getBrand()+":"+e.getTotal()));

    }

    //entry对象
    private static void countPrice(List<Purcase> list) {
        //1. 将同一品牌的purcase进行归类存储
        //key - 品牌
        //value - 该品牌对应的集合对象 - n个p
        Map<String,List<Purcase>> maps = new HashMap<>();

        //遍历list
        for (Purcase p : list) {
            String brand = p.getBrand();//品牌

            //判断该集合中是否已经存在该品牌
            if(maps.containsKey(brand)){
                //根据key来获取value
                List<Purcase> purcases = maps.get(brand);
                purcases.add(p);
            }else{
                List<Purcase> pList = new ArrayList<>();
                pList.add(p);
                maps.put(brand,pList);
            }
        }

        //统计出来正好是两列 - 直接利用map集合的第二种迭代方式
        //将品牌名和统计出来的总价封装到Entry对象

        //key - 品牌
        //value - 总价
        Map<String,Double> countMap = new HashMap<>();

        //统计
        Set<String> set = maps.keySet();
        for (String b : set) {
            double total = 0.0d;
            List<Purcase> lists = maps.get(b);
            for (Purcase purcase : lists) {
                total+=purcase.getCost();
            }
            //b => total
            countMap.put(b,total);
        }

        //根据total进行一个降序排.

        //countMap.forEach((k,v)-> System.out.println(k+":"+v));

        //为排序准备一个List
        List<Entry<String,Double>> sortList = new ArrayList<>();

        //集合的第二种迭代方式
        Set<Map.Entry<String, Double>> countSet = countMap.entrySet();
        Iterator<Map.Entry<String,Double>> countIter = countSet.iterator();
        while(countIter.hasNext()){
            Entry<String,Double> e = countIter.next();

            sortList.add(e);
        }

        sortList.sort((e1,e2)-> (int) (e2.getValue()-e1.getValue()));

        sortList.forEach(e-> System.out.println(e.getKey()+":"+e.getValue()));
    }
}
